package com.cloudshop.service.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudshop.service.common.dto.RestControllerResult;
import com.cloudshop.service.common.exception.LoginException;
import com.cloudshop.service.common.utils.CheckEmptyUtil;
import com.cloudshop.service.common.utils.JwtTokenUtil;
import com.cloudshop.service.user.dao.jpa.UserRepository;
import com.cloudshop.service.user.dto.user.LoginParamsDto;
import com.cloudshop.service.user.dto.user.LoginResultDto;
import com.cloudshop.service.user.dto.user.RegistryParamsDto;
import com.cloudshop.service.user.entity.Users;
import com.cloudshop.service.user.service.UserService;
import io.jsonwebtoken.Claims;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录Controller.
 *
 * @author 宫新程
 * @since 2019/4/1 19:11
 */
@Slf4j
@CrossOrigin
@RestController
public class LoginController {

  private static final String REDIS_PHONE_CODE_PREFIX = "phone_code:";

  @Resource private UserService userService;
  @Resource private UserRepository userRepository;
  @Resource private StringRedisTemplate stringRedisTemplate;

  /** 用户登录. */
  @PostMapping("/login")
  public RestControllerResult<LoginResultDto> login(@RequestBody LoginParamsDto paramsDto) {
    String verification = stringRedisTemplate.opsForValue().get(paramsDto.getYzmRedisKey());
    // 校验验证码
    if (CheckEmptyUtil.isEmpty(verification)
        || !paramsDto.getVerification().equalsIgnoreCase(verification)) {
      log.error("verification error ：{}, should is {}", paramsDto.getVerification(), verification);
      throw new LoginException("验证码错误");
    }
    return RestControllerResult.success(userService.login(paramsDto));
  }

  /** 用户注册. */
  @PostMapping("/register")
  public RestControllerResult<Boolean> register(@RequestBody RegistryParamsDto paramsDto) {
    // 校验短信验证码是否正确
    String phoneCode =
        stringRedisTemplate.opsForValue().get(REDIS_PHONE_CODE_PREFIX + paramsDto.getPhone());
    // 如果短信验证码错误
    if (CheckEmptyUtil.isEmpty(phoneCode) || !phoneCode.equals(paramsDto.getPhoneCode())) {
      throw new LoginException("短信验证码错误！");
    }
    return RestControllerResult.success(userService.register(paramsDto));
  }

  /** 发送短信验证码. */
  @PostMapping("/sendMessageCode")
  public RestControllerResult<Boolean> sendMessageCode(@RequestBody RegistryParamsDto paramsDto) {
    String verification = stringRedisTemplate.opsForValue().get(paramsDto.getYzmRedisKey());
    // 如果图片验证码错误
    if (CheckEmptyUtil.isEmpty(verification)
        || !verification.equalsIgnoreCase(paramsDto.getVerification())) {
      throw new LoginException("验证码错误");
    }
    // 判断该手机号是否已经被注册
    Users entity = userRepository.findByPhone(paramsDto.getPhone());
    if (CheckEmptyUtil.isNotEmpty(entity)) {
      throw new LoginException("该手机号已被注册");
    }
    return RestControllerResult.success(userService.sendMessageCode(paramsDto));
  }

  /** 根据token获取用户信息. */
  @PostMapping("/get-user-info-by-token")
  public RestControllerResult<Claims> getUserInfoByToken() {
    Claims claims = JwtTokenUtil.getClaims();
    System.out.println(JSONObject.toJSONString(claims));
    return RestControllerResult.success(claims);
  }
}
